package org.opens.mybatisplus.controller;

import cn.hutool.core.collection.CollectionUtil;
import org.opens.mybatisplus.entity.RecursiveQueryParam;
import org.opens.mybatisplus.pojo.DateTable;
import org.opens.mybatisplus.pojo.dto.MyBatisPerson;
import org.opens.mybatisplus.pojo.vo.ArrayParam;
import org.opens.util.R;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/api/request")
public class RequestParamController {

    @RequestMapping(value = "/modalAndAnnotation")
    public R useModal(
            MyBatisPerson person,
            @RequestParam(value = "source", required = true) Integer source
    ) {
        R ok = R.success("ok");
        ok.put("data1", person);
        ok.put("data2", source);
        return ok;
    }

    @RequestMapping(value = "/date")
    public R useData(
            @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam(value = "startDate", required = false) Date startDate
    ) {
        R ok = R.success("ok");
        ok.put("startDate", startDate);
        return ok;
    }

    @RequestMapping(value = "/hasDateEntity")
    public R useHasDateFiledEntity(
            DateTable dateTable
    ) {
        R ok = R.success("ok");
        ok.put("dateTable", dateTable);
        return ok;
    }

    /**
     * 功能:
     * 验证真实树结构是否可以接受
     */
    @RequestMapping(value = "/query")
    public R recursiveQuery(
            @RequestBody RecursiveQueryParam recursiveQueryParam
    ) {
        R ok = R.success("ok");
        ok.put("date", recursiveQueryParam);
        ok.put("date2", transform(recursiveQueryParam));
        return ok;
    }


    @RequestMapping(value = "/arr/param")
    public R requestWithArray(
            ArrayParam param
    ) {
        R ok = R.success("ok");
        ok.put("date", param);
        return ok;
    }

    public static List<RecursiveQueryParam> transform(RecursiveQueryParam realTree) {
        if (realTree == null) {
            return null;
        }
        List<RecursiveQueryParam> params = realTree.getRecursiveQueryParams();
        List<RecursiveQueryParam> result = new ArrayList<>();
        int initLevel = 1;

        RecursiveQueryParam paramsFirst = new RecursiveQueryParam();
        paramsFirst.setId(realTree.getId());
        paramsFirst.setRootId(realTree.getRootId());
        paramsFirst.setLevel(initLevel);
        result.add(paramsFirst);

        realTransform(params, result, initLevel);

        return result;
    }

    public static void realTransform(List<RecursiveQueryParam> realTree, List<RecursiveQueryParam> linearTree, int initLevel) {
        if (CollectionUtil.isNotEmpty(realTree)) {
            for (RecursiveQueryParam queryParam : realTree) {
                RecursiveQueryParam params = new RecursiveQueryParam();
                params.setLevel(initLevel + 1);
                params.setId(queryParam.getId());
                params.setRootId(queryParam.getRootId());
                linearTree.add(params);
                realTransform(queryParam.getRecursiveQueryParams(), linearTree, initLevel + 1);
            }
        }
    }

}